using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Ataric.Themester.Parser {
	/// <summary></summary>
    /// 
    [Serializable]
	public class CSSDocument : IRuleSetContainer {
		private string charset;
		private List<Directive> dirs = new List<Directive>();
		private List<RuleSet> rulesets = new List<RuleSet>();

		/// <summary></summary>
		[XmlArrayItem("Directive", typeof(Directive))]
		[XmlArray("Directives")]
		public List<Directive> Directives {
			get { return dirs; }
			set { dirs = value; }
		}

		/// <summary></summary>
		[XmlArrayItem("RuleSet", typeof(RuleSet))]
		[XmlArray("RuleSets")]
		public List<RuleSet> RuleSets {
			get { return rulesets; }
			set { rulesets = value; }
		}

		/// <summary></summary>
		/// <returns></returns>
		public override string ToString() {
			System.Text.StringBuilder txt = new System.Text.StringBuilder();
			foreach (Directive dr in dirs) {
				txt.AppendFormat("{0}\r\n", dr.ToString());
			}
			if (txt.Length > 0) { txt.Append("\r\n"); }
			foreach (RuleSet rules in rulesets) {
				txt.AppendFormat("{0}\r\n", rules.ToString());
			}
			return txt.ToString();
		}
        public string ToString(bool removeTab)
        {
            System.Text.StringBuilder txt = new System.Text.StringBuilder();
            foreach (Directive dr in dirs)
            {
                txt.AppendFormat("{0}\r\n", dr.ToString());
            }
            if (txt.Length > 0) { txt.Append("\r\n"); }
            foreach (RuleSet rules in rulesets)
            {
                txt.AppendFormat("{0}\r\n", rules.ToString(0,!removeTab));
            }
            return txt.ToString();
        }
        public CSSDocument SingleAll()
        {
            CSSDocument doc = new CSSDocument();

            foreach (var rule1 in RuleSets)
            {
                foreach (var s in rule1.Selectors)
                {

                    foreach (var ss in s.SimpleSelectors)
                    {
                        RuleSet r = null;
                        foreach (RuleSet rule2 in doc.RuleSets)
                        {
                            if (string.Compare(ss.ToString(), rule2.Selectors[0].ToString(), true) == 0)
                            {
                                r = rule2;
                                rule2.Declarations.AddRange(rule1.Declarations);
                            }
                        }
                        if (r == null)
                        {
                            r = new RuleSet()
                            {
                                Selectors = new List<Selector>() {
                                   new Selector() {
                                   SimpleSelectors = new List<SimpleSelector>() {ss}
                                   }
                                },
                            };
                            r.Declarations = rule1.Declarations;
                            doc.RuleSets.Add(r);
                        };
                    }

                }
            }
            return doc;
        }
	}
}